package cn.schoolwow.workflow.module.task.action.service;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDefinitionOption;
import cn.schoolwow.workflow.domain.task.type.AddSignTaskRequest;
import cn.schoolwow.workflow.entity.WorkFlowDefinition;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.task.action.flow.addSign.AfterAddSignTaskFlow;
import cn.schoolwow.workflow.module.task.action.flow.addSign.BeforeAddSignTaskFlow;
import cn.schoolwow.workflow.module.task.action.flow.common.CheckWorkFlowTaskExistFlow;

public class AddSignTaskServiceFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkRequest(flowContext);
        checkOption(flowContext);
        checkAddSignType(flowContext);
    }

    @Override
    public String name() {
        return "加签工作流实例任务服务流程";
    }

    private void checkRequest(FlowContext flowContext){
        AddSignTaskRequest addSignTaskRequest = flowContext.checkInstanceData(AddSignTaskRequest.class);

        CheckInstance.newCheck()
                .notNullCheck(addSignTaskRequest.taskId, "任务id")
                .notEmptyCheck(addSignTaskRequest.signAssignerList, "加签人")
                .notNullCheck(addSignTaskRequest.addSignType, "加签类型");
        flowContext.startFlow(new CheckWorkFlowTaskExistFlow())
                .putCurrentCompositeFlowData("taskId", addSignTaskRequest.taskId)
                .execute();
        if(null==addSignTaskRequest.signTaskName){
            WorkFlowTask workFlowTask = flowContext.checkInstanceData(WorkFlowTask.class);
            addSignTaskRequest.signTaskName = workFlowTask.getTaskName();
        }
    }

    private void checkOption(FlowContext flowContext){
        WorkFlowTask workFlowTask = flowContext.checkInstanceData(WorkFlowTask.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        Long definitionId = dao.query(WorkFlowInstance.class)
                .addQuery("id", workFlowTask.getInstanceId())
                .addColumn("definitionId")
                .execute()
                .getSingleColumn(Long.class);
        WorkFlowDefinition workFlowDefinition = dao.query(WorkFlowDefinition.class)
                .addQuery("id", definitionId)
                .addColumn("name")
                .addColumn("option")
                .execute()
                .getOne();
        if(null==workFlowDefinition||null==workFlowDefinition.getOption()){
            return;
        }
        WorkFlowDefinitionOption workFlowDefinitionOption = workFlowDefinition.getOption().toJavaObject(WorkFlowDefinitionOption.class);
        if(!workFlowDefinitionOption.allowAdd){
            throw new IllegalArgumentException("工作流程["+workFlowDefinition.getName()+"]不允许加签");
        }
    }

    private void checkAddSignType(FlowContext flowContext){
        AddSignTaskRequest addSignTaskRequest = flowContext.checkInstanceData(AddSignTaskRequest.class);

        switch (addSignTaskRequest.addSignType){
            case ADD_BEFORE_SIGN:{
                flowContext.executeFlowList(new BeforeAddSignTaskFlow());
            }break;
            case ADD_AFTER_SIGN:{
                flowContext.executeFlowList(new AfterAddSignTaskFlow());
            }break;
        }
    }
}
